package com.limou.controller;

import com.github.pagehelper.PageInfo;
import com.limou.pojo.EmpJob;
import com.limou.pojo.Employee;
import com.limou.service.EmployeeService;
import org.apache.taglibs.standard.lang.jstl.NullLiteral;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmpJob empJob;

    //查找所有员工
    @RequestMapping("/findAllEmployee")
    public String findAllEmployee(Model model) {
        System.out.println("查询所有员工controller层。。。。。。。。。。");
        List<Employee> allEmployee = employeeService.findAllEmployee();
        System.out.println(allEmployee);
        model.addAttribute("employeelist", allEmployee);
        return "allEmployee";
    }

    //添加员工，并页面重定向
    @RequestMapping("/addEmployeePage")
    public String addEmployeePage() {
        System.out.println("addEmployeePage。。。。。。。。。。");
        return "addEmployee";
    }

    @RequestMapping("/addEmployee")
    public String addEmployee(Employee employee) {
        System.out.println("addEmployee。。。。。。。。。。");
        employeeService.addEmployee(employee);
        return "forward:/employee/employeePage";
    }

    //修改员工信息
    @RequestMapping("/updateEmployeePage")
    public String updateEmployeePage(@RequestParam("emp_id") int id, Model model) {
        System.out.println("updateEmployeePage。。。。。。。。。。");
        Employee employeeById = employeeService.findAllEmployeeById(id);
        System.out.println(employeeById);
        model.addAttribute("employeeById", employeeById);
        return "updateEmployee";
    }

    @RequestMapping("/updateEmployee")
    public String updateEmployeePage(Employee employee, Model model) {
        System.out.println(employee+"aaaaaaaaa");
        employeeService.updateEmployee(employee);

        return "redirect:/employee/employeePage";
    }


    //删除员工
    @RequestMapping("/deleteEmployee")
    public String deleteEmployee(@RequestParam("emp_id") int id, Model model) {
        System.out.println("controller删除员工。。。。");
        Employee employee = employeeService.findAllEmployeeById(id);
        employeeService.deleteEmployee(employee);
        return "redirect:/employee/employeePage";
    }


    @RequestMapping("/findEmployeeByName")
    public String findEmployeeByName(String username, Model model) {
        System.out.println("通过姓名查找controller。。。。。。");
        List<Employee> employees = employeeService.findEmployeeByName(username);
        model.addAttribute("employees", employees);
        return "findEmployee";
    }

    //分页显示功能
    /*

     */
    @RequestMapping("/employeePage")
    public String getEmployeePage(Integer pageNum , Model model) {
        System.out.println("getEmployeePage。。。。。。。。。。");
        //判断显示员工页面，是否由其他页面重定向过来（若为重定向过来，则不会传递页数信息pageNum），将重定向过来的页面，显示在第一页
        System.out.println("");
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        PageInfo<Employee> employeePage = employeeService.getEmployeePage(pageNum);
        //PageInfo<Employee> employeePage，传递到查询所有员工页面allEmployee，进行操作
        model.addAttribute("page",employeePage);

        //跳转到查询所有员工页面allEmployee
        return "allEmployee";

    }


//    查找显示所有员工的职位
    @RequestMapping("/findEmployeeJob")
    public String findEmployeeJob( Model model) {
      List<Employee> list  =employeeService.findEmployeeJob();

        System.out.println(list);
        model.addAttribute("jobList" ,list);
        return "employeeJob";
    }
    @RequestMapping("/changeEmployeeJob")
    public String changeEmployeeJob(Integer emp_id, Model model) {

        System.out.println("changeEmployeeJob............");
        System.out.println(emp_id);
        Employee employee = employeeService.findAllEmployeeById(emp_id);
        System.out.println("这个员工是"+employee);
        model.addAttribute("employee" ,employee);

        return "updateEmployeeJob";
    }

    @RequestMapping("/changeEmployeeJobPage")
    public String changeEmployeeJobPage(Integer empId, Integer  job1,Integer job2 ,Integer job3,Integer job4,Integer job5,Model model) {
        System.out.println("员工id是。。。。。。。。。。。。。。。"+empId);
        model.addAttribute("empId" ,empId);
//        删除此id的数据
        employeeService.deleteEmpJobById(empId);
        List<Integer> list = new ArrayList<>();
        list.add(job1);
        list.add(job2);
        list.add(job3);
        list.add(job4);
        list.add(job5);
        System.out.println(empId+"添加list后的empId");
        System.out.println(list);
//        循环添加新数据
        for (Integer job:
        list){
            System.out.println(job);
            if (job != null){
                empJob.setEmpId(empId);
                empJob.setJobId(job);
                System.out.println(empJob);
                employeeService.insertEmpJobById(empJob);
            }

        }

        return "forward:findEmployeeJob";
    }
}
